<?php
function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

function taxonomy($tax_only, $dest_class_id = 0) {
//print_r($_GET);
//echo $_GET["fld"];
//echo $_GET["cop"];

  //$tmpfile = fopen("e:/1.txt","w");
  //fwrite($tmpfile, "aaA".$_GET["uid"]);
  //fclose($tmpfile);

  global $_GET, $data, $path, $style, $session_id, $language, $header, $term_Thing_ID;
  include "attr.php";
  include "acon.php";
  include "compatible.php";
  include "hierarchy.php";
  include "functional.php";
  include "alignment.php";
  include "synonyms.php";
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = "";

  //Data
  if (!isset($_GET["sel"])) $_GET["sel"] = -1;

  $ontoclass = 0;
  $ID[0] = $_GET["uid"];
  $flag = -1;
  switch (substr($_GET["uid"], 0, 1)) {
    case "r":
      $tmp_sql = "SELECT class.id as id, term.text as name ".
                            "FROM class, term ".
                            "WHERE class.parent_id = ".$_GET["thinguid"].
                            " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
                            " AND class.base_onto_id = ".substr($_GET["uid"], 2, strpos($_GET["uid"], "c") - 2).
                            " ORDER BY term.text;";
      $i_max = getMultData($tmp_sql, $ontosdata);
      $attrs = "";

    break;

    //Class is selected
    case "c":
      if (is_numeric(substr($_GET["uid"], 1)))
      $sql_tmp = "SELECT term_id FROM class WHERE id = ".substr($_GET["uid"], 1);
      $ontotype = Array();
      getData($sql_tmp, $term_type);

      if ($term_type["term_id"] == "36"){
        // if thing
        $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".substr($_GET["uid"], 1).")";
        $ontotype = Array();
        getData($sql_tmp, $ontotype);

        //Watch type of current ontology
        switch ($ontotype["type_id"]) {
          //Request Ontology
          case "5":
            $i_max = getMultData($sql_tmp = "SELECT o.id, t.text as name FROM ontology o, term t WHERE o.term_id=t.id and o.id in (SELECT DISTINCT base_onto_id FROM class WHERE parent_id = ".substr($_GET["uid"], 1).")", $ontosdata);
            $ontodata = "";
            $attrs = "";
            $ontoclass = 1;
            $OClassID = substr($_GET["uid"], 1);
          break;

          //Application Ontology
          case "0":
            $i_max = getMultData($sql_tmp = "SELECT o.id, t.text as name FROM ontology o, term t WHERE o.term_id=t.id and t.lang_id=".$language["content"]." and o.id in (SELECT DISTINCT base_onto_id FROM class WHERE parent_id = ".substr($_GET["uid"], 1).")", $ontosdata);
            $ontodata = "";
            $attrs = "";
            $ontoclass = 1;
            $OClassID = substr($_GET["uid"], 1);
          break;
          default:
            $i_max = getMultData($la_sql = "SELECT class.id as id, term.text as name ".
                                 "FROM class, term ".
                                 "WHERE class.parent_id = ".substr($_GET["uid"], 1).
                                 " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
                                 " ORDER BY term.text;", $ontosdata);
            //echo $i_max;

            $ontodata = "";
            $attrs = "";
            $sql_tmp = "SELECT * FROM class WHERE id = ".substr($_GET["uid"], 1)." AND term_id = ".$term_Thing_ID.";";
            //print $sql_tmp;
            $OClassID = "";

        };
      }else{
          $OClassID = "";
          //if other classes
          $i_max = getMultData("SELECT class.id as id, term.text as name ".
                               "FROM class, term ".
                               "WHERE class.parent_id = ".substr($_GET["uid"], 1).
                               " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
                               " ORDER BY term.text;", $ontosdata);
          $ontodata = "";
          $attrs = "";
          $sql_tmp = "SELECT * FROM class WHERE id = ".substr($_GET["uid"], 1)." AND term_id = ".$term_Thing_ID.";";
      };

      if (getData($sql_tmp, $thingdata) == -1){

        // Check for Ontology Type if TMO then IP OP attributes required
        $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".substr($_GET["uid"], 1).")";
        $ontotype = Array();
        getData($sql_tmp, $ontotype);

        switch ($ontotype["type_id"]) {
          case "0":
            /////////////
            //AO ONTOLOGY
            /////////////
            $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT base_onto_id FROM class WHERE id = ".substr($_GET["uid"], 1).")";
            $ontotype = Array();
            getData($sql_tmp, $ontotype);

            if ($ontotype["type_id"] == 2){//TMO Ontology
              if (substr($_GET["fld"], 0, 1) == 1){
                    //folder Input attributes
                    $tmp_attr = str_replace("%CLASS_ID%", "ia".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Input attributes"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                    $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                    if (getData("SELECT * FROM class_attr c, attr a WHERE c.class_id = ".str_replace("c","",$_GET["uid"])." and a.Type = 1 and c.attr_id=a.id;",
                      $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 1) > 0)
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=iattrlist&amp;sid=".
                        $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs = $tmp_attr;
              };
              if (substr($_GET["fld"], 1, 1) == 1){
                    //folder Output attributes
                    $tmp_attr = str_replace("%CLASS_ID%", "oa".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Output attributes"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                    $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                    if (getData("SELECT * FROM class_attr c, attr a WHERE c.class_id = ".str_replace("c","",$_GET["uid"])." and a.Type = 2 and c.attr_id=a.id;",
                      $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 2) > 0)
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=oattrlist&amp;sid=".
                        $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
              };
            };

            if ($ontotype["type_id"] == 1){//Domain Ontology
              if (substr($_GET["fld"], 2, 1) == 1){
                    //folder attributes
                    $tmp_attr = str_replace("%CLASS_ID%", "a".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Attributes"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                    $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                    if (getData("SELECT * FROM class_attr WHERE class_id = ".str_replace("c","",$_GET["uid"]).";",
                      $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 0) > 0)
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=attrlist&amp;sid=".
                        $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"]."&amp;bound=true", $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs = $tmp_attr;
              };
            };
            if (substr($_GET["fld"], 3, 1) == 1){
                    //folder acon
                    $tmp_attr = str_replace("%CLASS_ID%", "ac".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Associative Constraints"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=aconlistdata&amp;sid=".
                    $session_id."&amp;uid=ac".$_GET["uid"], $tmp_attr);
                    //if (getData("SELECT * FROM acon WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                    if (getAcon(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=aconlist&amp;sid=".
                        $session_id."&amp;uid=ac".str_replace("c","",$_GET["uid"]), $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
              };

              if (substr($_GET["fld"], 4, 1) == 1){
                    //folder comatible
                    $tmp_attr = str_replace("%CLASS_ID%", "com".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Compatibility"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=compatiblelistdata&amp;sid=".
                    $session_id."&amp;uid=com".$_GET["uid"], $tmp_attr);
                    //if (getData("SELECT * FROM compatible WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                    if (getCompatible(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=compatiblelist&amp;sid=".
                        $session_id."&amp;uid=com".str_replace("c","",$_GET["uid"]), $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
              };

              if (substr($_GET["fld"], 5, 1) == 1){
                    //folder hierarchy
                    $tmp_attr = str_replace("%CLASS_ID%", "h".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Hierarchy"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=hierarchylistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    //if (getData("SELECT * FROM hierarchy WHERE parent_class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
                    if (getHierarchy(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=hierarchylist&amp;sid=".
                        $session_id."&amp;uid=h".str_replace("c","",$_GET["uid"]), $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
               };

               if (substr($_GET["fld"], 6, 1) == 1){
                    //folder functional constraints
                    $tmp_attr = str_replace("%CLASS_ID%", "funclist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Functional constraints"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=functionallistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    if (getFunctional(str_replace("c", "", $_GET["uid"]), $matches, $session_id, "class") != "")
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=functionallist&amp;sid=".
                      $session_id."&amp;uid=funclist".str_replace("c","",$_GET["uid"]), $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
               };

               if (substr($_GET["fld"], 8, 1) == 1){
                    //folder synonyms
                    $tmp_attr = str_replace("%CLASS_ID%", "synlist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Synonyms"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=synonymlistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    if (getSynonyms(str_replace("c", "", $_GET["uid"]), $matches, $session_id, "class") != "")
                      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=synonymlist&amp;sid=".
                      $session_id."&amp;uid=synlist".str_replace("c","",$_GET["uid"]), $tmp_attr);
                    else
                      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    $attrs .= $tmp_attr;
               };
               if (substr($_GET["fld"], 9, 1) == 1 && $ontotype["type_id"] == 2){
                    //folder WS
                    $tmp_attr = str_replace("%CLASS_ID%", "wslist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Web-Service"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=wslistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    
                    if (getData("SELECT * FROM web_service WHERE class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
	                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=wslist&amp;sid=".
	                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
	                else
	                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    
                    $attrs .= $tmp_attr;
               };
              break;

          break;
          case "2":
            //////////////
            //TMO Ontology
            //////////////
            if (substr($_GET["fld"], 0, 1) == 1){
                //folder Input attributes
                $tmp_attr = str_replace("%CLASS_ID%", "ia".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Input attributes"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                if (getData("SELECT * FROM class_attr c, attr a WHERE c.class_id = ".str_replace("c","",$_GET["uid"])." and a.Type = 1 and c.attr_id=a.id;",
                  $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 1) > 0)
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=iattrlist&amp;sid=".
                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs = $tmp_attr;
            };

            if (substr($_GET["fld"], 1, 1) == 1){
                //folder Output attributes
                $tmp_attr = str_replace("%CLASS_ID%", "oa".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Output attributes"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                if (getData("SELECT * FROM class_attr c, attr a WHERE c.class_id = ".str_replace("c","",$_GET["uid"])." and a.Type = 2 and c.attr_id=a.id;",
                  $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 2) > 0)
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=oattrlist&amp;sid=".
                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 3, 1) == 1){
                //folder acon
                $tmp_attr = str_replace("%CLASS_ID%", "ac".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Associative Constraints"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=aconlistdata&amp;sid=".
                $session_id."&amp;uid=ac".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM acon WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                if (getAcon(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=aconlist&amp;sid=".
                    $session_id."&amp;uid=ac".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 5, 1) == 1){
                //folder hierarchy
                $tmp_attr = str_replace("%CLASS_ID%", "h".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Hierarchy"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=hierarchylistdata&amp;sid=".
                $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM hierarchy WHERE parent_class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
                if (getHierarchy(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=hierarchylist&amp;sid=".
                    $session_id."&amp;uid=h".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };
             if (substr($_GET["fld"], 8, 1) == 1){
                  //folder synonyms
                  $tmp_attr = str_replace("%CLASS_ID%", "synlist".str_replace("c","",$_GET["uid"]), $matches[1]);
                  $tmp_attr = str_replace("%CLASS_NM%", getMessage("Synonyms"), $tmp_attr);
                  $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                  $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=synonymlistdata&amp;sid=".
                  $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                  if (getSynonyms(str_replace("c", "", $_GET["uid"]), $matches, $session_id, "class") != "")
                    $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=synonymlist&amp;sid=".
                    $session_id."&amp;uid=synlist".str_replace("c","",$_GET["uid"]), $tmp_attr);
                  else
                    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                  $attrs .= $tmp_attr;
             };
             if (substr($_GET["fld"], 9, 1) == 1 && $ontotype["type_id"] == 2){
                    //folder WS
                    $tmp_attr = str_replace("%CLASS_ID%", "wslist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Web-Service"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=wslistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    
                    if (getData("SELECT * FROM web_service WHERE class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
	                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=wslist&amp;sid=".
	                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
	                else
	                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    
                    $attrs .= $tmp_attr;
               };
            break;
          case "5":
            //////////////////
            //Request ontology
            //////////////////
            if (substr($_GET["fld"], 2, 1) == 1){
                //folder attributes
                $tmp_attr = str_replace("%CLASS_ID%", "a".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Attributes"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                if (getData("SELECT * FROM class_attr WHERE class_id = ".str_replace("c","",$_GET["uid"]).";",
                  $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 0) > 0)
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=attrlist&amp;sid=".
                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs = $tmp_attr;
            };

            if (substr($_GET["fld"], 3, 1) == 1){
                //folder acon
                $tmp_attr = str_replace("%CLASS_ID%", "ac".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Associative Constraints"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=aconlistdata&amp;sid=".
                $session_id."&amp;uid=ac".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM acon WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                if (getAcon(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=aconlist&amp;sid=".
                    $session_id."&amp;uid=ac".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 4, 1) == 1){
                //folder comatible
                $tmp_attr = str_replace("%CLASS_ID%", "com".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Compatibility"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=compatiblelistdata&amp;sid=".
                $session_id."&amp;uid=com".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM compatible WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                if (getCompatible(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=compatiblelist&amp;sid=".
                    $session_id."&amp;uid=com".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 5, 1) == 1){
                //folder hierarchy
                $tmp_attr = str_replace("%CLASS_ID%", "h".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Hierarchy"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=hierarchylistdata&amp;sid=".
                $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM hierarchy WHERE parent_class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
                if (getHierarchy(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=hierarchylist&amp;sid=".
                    $session_id."&amp;uid=h".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 7, 1) == 1){
                //folder alignment
                $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT base_onto_id FROM class WHERE id = ".substr($_GET["uid"], 1).")";
                $ontotype = Array();
                getData($sql_tmp, $ontotype);
                if ($ontotype["type_id"] == 3){
                  $tmp_attr = str_replace("%CLASS_ID%", "alc".str_replace("c","",$_GET["uid"]), $matches[1]);
                  $tmp_attr = str_replace("%CLASS_NM%", getMessage("Alignment"), $tmp_attr);
                  $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                  $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=alignmentlistdata&amp;sid=".
                  $session_id."&amp;uid=alc".$_GET["uid"], $tmp_attr);
                  if (getAlignment(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                    $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=alignmentlist&amp;sid=".
                      $session_id."&amp;uid=alc".str_replace("c","",$_GET["uid"]), $tmp_attr);
                  else
                    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                  $attrs .= $tmp_attr;
                };
            };
            if (substr($_GET["fld"], 8, 1) == 1){
                //folder synonyms
                $tmp_attr = str_replace("%CLASS_ID%", "synlist".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Synonyms"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=synonymlistdata&amp;sid=".
                $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                if (getSynonyms(str_replace("c", "", $_GET["uid"]), $matches, $session_id, "class") != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=synonymlist&amp;sid=".
                  $session_id."&amp;uid=synlist".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };
            if (substr($_GET["fld"], 9, 1) == 1 && $ontotype["type_id"] == 2){
                    //folder WS
                    $tmp_attr = str_replace("%CLASS_ID%", "wslist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Web-Service"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=wslistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    
                    if (getData("SELECT * FROM web_service WHERE class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
	                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=wslist&amp;sid=".
	                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
	                else
	                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    
                    $attrs .= $tmp_attr;
               };

            break;
          default:
            //////////////////
            //Other ontologies (Domain ontology, Preliminary request ontology, Preliminary Knowledge Source Ontology, Knowledge Source Ontology)
            //////////////////
            if (substr($_GET["fld"], 2, 1) == 1){
                //folder attributes
                $tmp_attr = str_replace("%CLASS_ID%", "a".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Attributes"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=attrlistdata&amp;sid=".
                $session_id."&amp;uid=c".$_GET["uid"], $tmp_attr);
                if (getData("SELECT * FROM class_attr WHERE class_id = ".str_replace("c","",$_GET["uid"]).";",
                  $ontodata) == 1 || getAttributes(str_replace("c","",$_GET["uid"]), "", 0, 0) > 0)
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=attrlist&amp;sid=".
                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs = $tmp_attr;
            };
            if (substr($_GET["fld"], 3, 1) == 1){
                //folder acon
                $tmp_attr = str_replace("%CLASS_ID%", "ac".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Associative Constraints"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=aconlistdata&amp;sid=".
                $session_id."&amp;uid=ac".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM acon WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                if (getAcon(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=aconlist&amp;sid=".
                    $session_id."&amp;uid=ac".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 4, 1) == 1){
                //folder comatible
                $tmp_attr = str_replace("%CLASS_ID%", "com".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Compatibility"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=compatiblelistdata&amp;sid=".
                $session_id."&amp;uid=com".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM compatible WHERE class1_id = ".str_replace("c","",$_GET["uid"])." or class2_id = ".str_replace("c","",$_GET["uid"]).";", $ontodata) == 1)
                if (getCompatible(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=compatiblelist&amp;sid=".
                    $session_id."&amp;uid=com".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                $attrs .= $tmp_attr;
            };

            if (substr($_GET["fld"], 5, 1) == 1){
                //folder hierarchy
                $tmp_attr = str_replace("%CLASS_ID%", "h".str_replace("c","",$_GET["uid"]), $matches[1]);
                $tmp_attr = str_replace("%CLASS_NM%", getMessage("Hierarchy"), $tmp_attr);
                $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=hierarchylistdata&amp;sid=".
                $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                //if (getData("SELECT * FROM hierarchy WHERE parent_class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
                if (getHierarchy(str_replace("c","",$_GET["uid"]), $matches, $session_id) != "")
                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=hierarchylist&amp;sid=".
                    $session_id."&amp;uid=h".str_replace("c","",$_GET["uid"]), $tmp_attr);
                else
                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);

                $attrs .= $tmp_attr;
            };

             if (substr($_GET["fld"], 8, 1) == 1){
                  //folder synonyms
                  $tmp_attr = str_replace("%CLASS_ID%", "synlist".str_replace("c","",$_GET["uid"]), $matches[1]);
                  $tmp_attr = str_replace("%CLASS_NM%", getMessage("Synonyms"), $tmp_attr);
                  $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                  $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=synonymlistdata&amp;sid=".
                  $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                  if (getSynonyms(str_replace("c", "", $_GET["uid"]), $matches, $session_id, "class") != "")
                    $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=synonymlist&amp;sid=".
                    $session_id."&amp;uid=synlist".str_replace("c","",$_GET["uid"]), $tmp_attr);
                  else
                    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                  $attrs .= $tmp_attr;
             };
             if (substr($_GET["fld"], 9, 1) == 1 && $ontotype["type_id"] == 2){
                    //folder WS
                    $tmp_attr = str_replace("%CLASS_ID%", "wslist".str_replace("c","",$_GET["uid"]), $matches[1]);
                    $tmp_attr = str_replace("%CLASS_NM%", getMessage("Web-Service"), $tmp_attr);
                    $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
                    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=wslistdata&amp;sid=".
                    $session_id."&amp;uid=h".$_GET["uid"], $tmp_attr);
                    
                    if (getData("SELECT * FROM web_service WHERE class_id = ".str_replace("c","",$_GET["uid"]), $ontodata) == 1)
	                  $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=wslist&amp;sid=".
	                    $session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $tmp_attr);
	                else
	                  $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
                    
                    $attrs .= $tmp_attr;
               };
          };
        };

        //$attrs = getAttributes(substr($_GET["uid"], 1), $matches[1], 1);
        break;
    default:
      $open_folders = "";
      if (isset($_GET["oiattr"]) && $_GET["oiattr"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["ooattr"]) && $_GET["ooattr"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["oattr"]) && $_GET["oattr"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["oacon"]) && $_GET["oacon"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["ocomp"]) && $_GET["ocomp"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["oh"]) && $_GET["oh"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["ofunc"]) && $_GET["ofunc"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["oalignment"]) && $_GET["oalignment"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
      if (isset($_GET["osyn"]) && $_GET["osyn"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";

      //class operations
      if (isset($_GET["ocop"]) && $_GET["ocop"] == 1)
        $class_operations = 1;
      else
        $class_operations = 0;
        
      if (isset($_GET["ows"]) && $_GET["ows"] == 1)
        $open_folders .= "1";
      else
        $open_folders .= "0";
        
      $i_max = getMultData($sql="SELECT class.id as id, term.text as name ".
        "FROM class, term ".
        "WHERE class.onto_id = ".$_GET["uid"].
        " AND class.parent_id = 0 AND class.term_id = term.id AND term.lang_id = ".$language["content"].
        " ORDER BY term.text;",
        $ontosdata);

      // flag for open subclass
      $sql_tmp = "SELECT * FROM class WHERE parent_id = ".$ontosdata[0]["id"];
      if ((getData($sql_tmp, $thingdata) == -1) && ($open_folders == "000000000"))
        $flag = 0;
      else
        $flag = 1;
      $attrs = "";
      break;
  }

  if (isset($_GET["fld"]))
     $open_folders = $_GET["fld"];
  $contents = str_replace("%FLD_VIS%", $open_folders, $contents);
  if (isset($_GET["cop"]))
     $class_operations = $_GET["cop"];
  $contents = str_replace("%CLASSOPERATIONS%", $class_operations, $contents);

  if ($tax_only == 1)
    $rep_cont .= $attrs;

  //determine destination class id
  if ($dest_class_id == 0 && isset($_GET["dest_id"])){
    $dest_class_id = $_GET["dest_id"];
  };
  //echo "destination=".$dest_class_id." imax=".$i_max."<br />";

  for ($i = 0; $i < $i_max; $i++) {
    if ($OClassID != "")
      $ontosdata[$i]["id"] = "o".$ontosdata[$i]["id"]."c".$OClassID;
    if ($flag == -1){
      //echo $ontosdata[$i]["id"];
      if (!is_numeric($ontosdata[$i]["id"])){
        // if application ontology
        $sql_tmp = "SELECT * FROM class WHERE base_onto_id = ".substr($ontosdata[$i]["id"], 1, strpos($ontosdata[$i]["id"], "c") - 1)." and onto_id = (SELECT onto_id FROM class WHERE id = ".substr($ontosdata[$i]["id"], strpos($ontosdata[$i]["id"], "c") + 1).") and parent_id = ".substr($ontosdata[$i]["id"], strpos($ontosdata[$i]["id"], "c") + 1);
        $i_childs = getMultData($sql_tmp, $childs_classes);

        $i_childs = 1;
      }else{
        $sql_tmp = "SELECT * FROM class WHERE parent_id = ".$ontosdata[$i]["id"];
        $i_childs = getMultData($sql_tmp, $childs_classes);
        //echo $i_childs;
      };
    };
    // remove html special characters
    $ontosdata[$i]["name"] = htmlspecialchars($ontosdata[$i]["name"]);

    if ($ontoclass == 0){

      $tmp_cont = str_replace("%CLASS_ID%", "c".$ontosdata[$i]["id"], $matches[1]);
      $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
      $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=classdata&amp;sid=".
                        $session_id."&amp;uid=c".$ontosdata[$i]["id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);

      // open classes in ontology
      if ($dest_class_id != $ontosdata[$i]["id"]){
        //echo $dest_class_id;
        getdata($sql = "SELECT parent_id from class WHERE id = ".$dest_class_id, $parent_class_id);
        while ($parent_class_id["parent_id"] > 0 && $parent_class_id["parent_id"] != $ontosdata[$i]["id"]){
          getdata("SELECT parent_id from class WHERE id = ".$parent_class_id["parent_id"], $parent_class_id);
        };
        if ($parent_class_id["parent_id"] == $ontosdata[$i]["id"]){
          $tmp_cont = str_replace("%ACT%", "expand".$dest_class_id, $tmp_cont);
        };
      }else
        $tmp_cont = str_replace("%ACT%", "selectexpand".$dest_class_id, $tmp_cont);
      // end open

      if (($flag == 0) || (($flag == -1) && ($i_childs == 0) && ($open_folders == "000000000")))
          $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);
      else
        if ($tax_only == 1)
          $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=tax&amp;sid=".
            $session_id."&amp;uid=c".$ontosdata[$i]["id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);
        else
          $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=2dview&amp;sid=".
            $session_id."&amp;uid=c".$ontosdata[$i]["id"], $tmp_cont);
    }else{
      $tmp_cont = str_replace("%CLASS_ID%", $ontosdata[$i]["id"], $matches[1]);

      // open ontologys in hybrid ontology
      $tmp_cont = str_replace("%ACT%", "expand".$dest_class_id, $tmp_cont);
      // end open

      $tmp_cont = str_replace("%IMG_TYPE%", "ontology", $tmp_cont);
      $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=ontodata&amp;sid=".
            $session_id."&amp;uid=".$ontosdata[$i]["id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);
      if (($flag == 0) || (($flag == -1) && ($i_childs == 0) && ($open_folders == "000000000")))
          $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);
      else
        if ($tax_only == 1)
          $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=tax&amp;sid=".
            $session_id."&amp;uid=r".$ontosdata[$i]["id"]."&amp;thinguid=".substr($_GET["uid"], 1)."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);
        else
          $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=2dview&amp;sid=".
            $session_id."&amp;uid=c".$ontosdata[$i]["id"], $tmp_cont);
    };
    $tmp_cont = str_replace("%CLASS_NM%", $ontosdata[$i]["name"], $tmp_cont);
    if ($_GET["sel"] == "c".$ontosdata[$i]["id"])
      $tmp_cont = str_replace("%ACT%", "select", $tmp_cont);
    //if ($_GET["sel"] == "0" && $i == 0)
    //if ((!isset($_GET["sel"]) || (isset($_GET["sel"]) && ($_GET["sel"] == "0" || $_GET["sel"] == "-1"))) && $i == 0)
    if ((!isset($_GET["sel"]) || (isset($_GET["sel"]) && $_GET["sel"] == "0")) && $i == 0)
      $tmp_cont = str_replace("%ACT%", "select", $tmp_cont);

    $rep_cont .= $tmp_cont;
  }

  $contents = str_replace($matches[0], $rep_cont, $contents);
  $contents = str_replace("%SELECT%", "c".$ontosdata[0]["id"], $contents);
  header ($header["xml"]);
  return $contents;
}

function classData() {
  global $_GET, $data, $path, $style, $session_id, $header;
  $_GET["uid"] = substr($_GET["uid"], 1);
  $contents = getClassData(1, "");
  header ($header["xml"]);
  return $contents;
}

function delete_double_attrs($class_id){
  $sql_str = "SELECT a.id, t.text FROM class_attr c, attr a, term t WHERE c.attr_id = a.id and t.id = a.term_id and t.lang_id = 0 and c.class_id = ".$class_id;
  $j_max = getMultData($sql_str, $attributes);

  $sql_str = "SELECT parent_id FROM class WHERE id = ".$class_id;
  getData($sql_str, $new_class_id);
  while ($new_class_id["parent_id"] > 0){
    $sql_str = "SELECT a.id, t.text FROM class_attr c, attr a, term t WHERE c.attr_id = a.id and t.id = a.term_id and t.lang_id = 0 and c.class_id = ".$new_class_id["parent_id"];
    $i_max = getMultData($sql_str, $attrs);
    for ($i = 0; $i < $i_max; $i++){
      $inh_attrs[count($inh_attrs)] = $attrs[$i]['text'];
    };
    $sql_str = "SELECT parent_id FROM class WHERE id = ".$new_class_id["parent_id"];
    getData($sql_str, $new_class_id);
  };

  for ($i = 0; $i < $j_max; $i++){
    //echo "!".$attributes[$i]['text']."!<br />";
    for ($j = 0; $j < count($inh_attrs); $j++){
      //echo $inh_attrs[$j]." ";
      if ($attributes[$i]['text'] == $inh_attrs[$j]){
        $sql_str = "DELETE FROM class_attr WHERE attr_id=".$attributes[$i]['id'];
        insertRecord($sql_str);
        $sql_str = "DELETE FROM attr WHERE id = ".$attributes[$i]['id'];
        insertRecord($sql_str);
      };
    };
  };
};

function delete_double_synonyms($class_id){
  $sql_str = "SELECT s.id, s.text FROM class c, synonyms s WHERE s.term_id = c.term_id and c.id = ".$class_id;
  $j_max = getMultData($sql_str, $synonyms);
  for ($j = 0; $j < $j_max; $j++){
    for ($i = $j + 1; $i < $j_max; $i++){
      if ($synonyms[$i]['text'] == $synonyms[$j]['text']){
        $sql_str = "DELETE FROM synonyms WHERE id=".$synonyms[$j]['id'];
        insertRecord($sql_str);
      };
    };
  };
};

function updateClass() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;
  include "settings.php";
  include "stemming.php";
  $_GET["uid"] = substr($_GET["uid"], 1);
  $comment = "";

  // change name and description
  $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$_GET["uid"].")";
  $ontotype = Array();
  getData($sql_tmp, $ontotype);
  if ((!($ontotype["type_id"] == "5")) && $data["type"] >= 1 && isset($_GET["oname"]) && isset($_GET["odescr"])) {
    $_GET["oname"] = utf8Encode($_GET["oname"]);
    $_GET["odescr"] = utf8Encode($_GET["odescr"]);
    getData("SELECT term_id FROM class WHERE id = ".$_GET["uid"].";", $ontoterm);
    if ($ontoterm["term_id"] != $term_Thing_ID) {//it is not Thing
      if ($_GET["oname"] == "") {
        $comment = getMessage("EmptyName");
      }else{
        $stem_text = stemming($_GET["oname"], $accent_path, $accent_lang);
        $stem_descr = stemming($_GET["odescr"], $accent_path, $accent_lang);

        $sql_str = "UPDATE term SET term.text = '".$_GET["oname"]."', term.description = '".$_GET["odescr"]."', term.stem_text = '".$stem_text."', term.stem_description = '".$stem_descr."' WHERE term.id = ".$ontoterm["term_id"]." AND term.lang_id = ".$language["content"];
        insertRecord($sql_str);
      }
    }
    $contents = getClassData(0, $comment);
    header ($header["xml"]);
    return $contents;
  }

  // transferring classes
  if ($data["type"] >= 1 && isset($_GET["otrans"])) {
    $_GET["otrans"] = utf8Encode($_GET["otrans"]);
    getdata("SELECT parent_id, onto_id FROM class WHERE id = ".$_GET["uid"], $parent_class_id);
    $class_id = $parent_class_id["parent_id"];
    //echo $_GET["uid"]." ".$class_id;

    if ($_GET["otrans"] <> $_GET["uid"]){
      $sql_str = "SELECT parent_id FROM class WHERE id = ".$_GET["otrans"];
      getData($sql_str, $parent_class);
      while ($parent_class["parent_id"] > 0 && $parent_class["parent_id"] <> $_GET["uid"]){
        $sql_str = "SELECT parent_id FROM class WHERE id = ".$parent_class["parent_id"];
        getData($sql_str, $parent_class);
      };
      if ($parent_class["parent_id"] == 0){
        $sql_str = "UPDATE class SET parent_id = '".$_GET["otrans"]."' WHERE id = ".$_GET["uid"];
        insertRecord($sql_str);

        $sql_str = "DELETE FROM hierarchy WHERE (parent_class_id = ".$_GET["uid"]." and child_class_id = ".$_GET["otrans"].") or (parent_class_id = ".$_GET["otrans"]." and child_class_id = ".$_GET["uid"].")";
        insertRecord($sql_str);

        delete_double_attrs($_GET["uid"]);

        //for pashkin
        $sql_str = "UPDATE session SET default_subclass = ".$_GET["otrans"]." WHERE session_id = '".$_GET["sid"]."'";
        insertRecord($sql_str);
      }else{
        $comment = getMessage("This class is child of transfered class");
      }
    }
    $_GET["uid"] = $parent_class_id["onto_id"];
    //echo "$class_id - ". $_GET["dest_id"];
    return taxonomy(1, $class_id);

  }

  // merge classes
  if ($data["type"] >= 1 && isset($_GET["ojoin"])) {
    $_GET["ojoin"] = utf8Encode($_GET["ojoin"]);
    getdata("SELECT parent_id, onto_id FROM class WHERE id = ".$_GET["uid"], $parent_class_id);
    $class_id = $parent_class_id["parent_id"];

    if ($_GET["ojoin"] <> $_GET["uid"]){

      // not taxonomycal relation
      $taxonomycal_relation = 1;
      $class_thing = 0;
      $sql_str = "SELECT parent_id FROM class WHERE id = ".$_GET["ojoin"];
      getData($sql_str, $parent_class);
      if ($parent_class["parent_id"] == 0)
        $class_thing = 1;
      while ($parent_class["parent_id"] > 0 && $parent_class["parent_id"] <> $_GET["uid"]){
        $sql_str = "SELECT parent_id FROM class WHERE id = ".$parent_class["parent_id"];
        getData($sql_str, $parent_class);
      };
      if ($parent_class["parent_id"] == 0) {
        $sql_str = "SELECT parent_id FROM class WHERE id = ".$_GET["uid"];
        getData($sql_str, $parent_class);
        if ($parent_class["parent_id"] == 0)
          $class_thing = 1;

        while ($parent_class["parent_id"] > 0 && $parent_class["parent_id"] <> $_GET["ojoin"]){
          $sql_str = "SELECT parent_id FROM class WHERE id = ".$parent_class["parent_id"];
          getData($sql_str, $parent_class);
      };
        if ($parent_class["parent_id"] == 0) {
          $taxonomycal_relation = 0;
        };
      }

      // not thing
        //on previos operation $class_thing

      // not hierarchy
      $sql_str = "SELECT count(*) as count_hierarchy FROM hierarchy WHERE parent_class_id = ".$_GET["ojoin"]." and child_class_id = ".$_GET["uid"]." or child_class_id = ".$_GET["ojoin"]." and parent_class_id = ".$_GET["uid"];
      getData($sql_str, $count_hierarchy);
      if ($count_hierarchy["count_hierarchy"] > 0)
        $hierarchy_relation = 1;
      else
        $hierarchy_relation = 0;

      // not noncompatible
      $sql_str = "SELECT count(*) as count_incompatible FROM compatible WHERE is_compatible <> 1 and ((class1_id = ".$_GET["ojoin"]." and class2_id = ".$_GET["uid"].") or (class2_id = ".$_GET["ojoin"]." and class1_id = ".$_GET["uid"]."))";
      getData($sql_str, $count_incompatible);
      if ($count_incompatible["count_incompatible"] > 0)
        $incompatible_relation = 1;
      else
        $incompatible_relation = 0;

      // not hyerarchy from choosen to O1 and from O1 to marked
      $sql_str = "SELECT count(*) as O1hierarchy FROM hierarchy WHERE (child_class_id = ".$_GET["uid"]." and parent_class_id in (SELECT child_class_id FROM hierarchy WHERE parent_class_id = ".$_GET["ojoin"]." )) or (parent_class_id = ".$_GET["uid"]." and child_class_id in (SELECT parent_class_id FROM hierarchy WHERE child_class_id = ".$_GET["ojoin"]." ))";
      getData($sql_str, $O1hierarchy);
      if ($O1hierarchy["O1hierarchy"] > 0)
        $O1hierarchy = 1;
      else
        $O1hierarchy = 0;

      // not compatible from chosen to O1 and incompatible from O1 to marked and back
      $sql_str = "SELECT count(*) as O1compatible FROM compatible WHERE ".
                  "(is_compatible <> 1 and class1_id = ".$_GET["uid"]." and class2_id in (SELECT class1_id FROM compatible WHERE is_compatible = 1 and class2_id = ".$_GET["ojoin"]." )) or ".
                  "(is_compatible = 1 and class1_id = ".$_GET["uid"]." and class2_id in (SELECT class1_id FROM compatible WHERE is_compatible <> 1 and class2_id = ".$_GET["ojoin"]." )) or ".

                  "(is_compatible <> 1 and class2_id = ".$_GET["uid"]." and class1_id in (SELECT class2_id FROM compatible WHERE is_compatible = 1 and class1_id = ".$_GET["ojoin"]." )) or ".
                  "(is_compatible = 1 and class2_id = ".$_GET["uid"]." and class1_id in (SELECT class2_id FROM compatible WHERE is_compatible <> 1 and class1_id = ".$_GET["ojoin"]." )) or ".

                  "(is_compatible = 1 and class1_id = ".$_GET["uid"]." and class2_id in (SELECT class2_id FROM compatible WHERE is_compatible <> 1 and class1_id = ".$_GET["ojoin"]." )) or ".
                  "(is_compatible <> 1 and class1_id = ".$_GET["uid"]." and class2_id in (SELECT class2_id FROM compatible WHERE is_compatible = 1 and class1_id = ".$_GET["ojoin"]." )) or ".

                  "(is_compatible = 1 and class2_id = ".$_GET["uid"]." and class1_id in (SELECT class1_id FROM compatible WHERE is_compatible <> 1 and class2_id = ".$_GET["ojoin"]." )) or ".
                  "(is_compatible <> 1 and class2_id = ".$_GET["uid"]." and class1_id in (SELECT class1_id FROM compatible WHERE is_compatible = 1 and class2_id = ".$_GET["ojoin"]." ))";
      getData($sql_str, $O1compatible);
      if ($O1compatible["O1compatible"] > 0)
        $O1compatible = 1;
      else
        $O1compatible = 0;

      if ($taxonomycal_relation == 0 && $class_thing == 0 && $hierarchy_relation == 0 && $incompatible_relation == 0 && $O1hierarchy == 0 && $O1compatible == 0){
        // update chidren choosen class to marked
        $sql_str = "UPDATE class SET parent_id = '".$_GET["ojoin"]."' WHERE parent_id = ".$_GET["uid"];
        insertRecord($sql_str);

        // delete associative relations between choosen and marked classes
        $sql_str = "DELETE from acon WHERE (class1_id = ".$_GET["ojoin"]." and class2_id = ".$_GET["uid"].") or (class2_id = ".$_GET["ojoin"]." and class1_id = ".$_GET["uid"].")";
        insertRecord($sql_str);

        // delete compatible relations between choosen and marked classes
        $sql_str = "DELETE from compatible WHERE (class1_id = ".$_GET["ojoin"]." and class2_id = ".$_GET["uid"].") or (class2_id = ".$_GET["ojoin"]." and class1_id = ".$_GET["uid"].")";
        insertRecord($sql_str);

        // insert to choosen class acon relations from marked
        $sql_str = "SELECT id FROM acon WHERE class1_id = ".$_GET["uid"]." and class2_id not in (SELECT class2_id FROM acon WHERE class1_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $acon_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE acon SET class1_id = ".$_GET["ojoin"]." WHERE id = ".$acon_data[$i]["id"];
          insertRecord($sql_str);
        };
        $sql_str = "SELECT id FROM acon WHERE class2_id = ".$_GET["uid"]." and class1_id not in (SELECT class1_id FROM acon WHERE class2_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $acon_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE acon SET class2_id = ".$_GET["ojoin"]." WHERE id = ".$acon_data[$i]["id"];
          insertRecord($sql_str);
        };

        $sql_str = "SELECT id FROM compatible WHERE class1_id = ".$_GET["uid"]." and class2_id not in (SELECT class2_id FROM compatible WHERE class1_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $compatible_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE compatible SET class1_id = ".$_GET["ojoin"]." WHERE id = ".$compatible_data[$i]["id"];
          insertRecord($sql_str);
        };
        $sql_str = "SELECT id FROM compatible WHERE class2_id = ".$_GET["uid"]." and class1_id not in (SELECT class1_id FROM compatible WHERE class2_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $compatible_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE compatible SET class2_id = ".$_GET["ojoin"]." WHERE class2_id = ".$compatible_data[$i]["id"];
          insertRecord($sql_str);
        };

        $sql_str = "SELECT id FROM hierarchy WHERE parent_class_id = ".$_GET["uid"]." and child_class_id not in (SELECT child_class_id FROM compatible WHERE parent_class_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $hierarchy_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE hierarchy SET parent_class_id = ".$_GET["ojoin"]." WHERE id = ".$hierarchy_data[$i]["id"];
          insertRecord($sql_str);
        }
        $sql_str = "SELECT id FROM hierarchy WHERE child_class_id = ".$_GET["uid"]." and parent_class_id not in (SELECT parent_class_id FROM compatible WHERE child_class_id = ".$_GET["ojoin"].")";
        $i_max = getMultData($sql_str, $hierarchy_data);
        for ($i = 0; $i < $i_max; $i++){
          $sql_str = "UPDATE hierarchy SET child_class_id = ".$_GET["ojoin"]." WHERE id = ".$hierarchy_data[$i]["id"];
          insertRecord($sql_str);
        };

        $sql_str = "UPDATE func_constr SET relation_id = ".$_GET["ojoin"]." WHERE relation_type = 0 and relation_id = ".$_GET["uid"];
        insertRecord($sql_str);

        $sql_str = "DELETE FROM class WHERE id = ".$_GET["uid"];
        insertRecord($sql_str);

        delete_double_attrs($_GET["ojoin"]);
      }else{
        $comment = getMessage("This class is child of transfered class");
      }
    }
    $_GET["uid"] = $parent_class_id["onto_id"];
    return taxonomy(1, $class_id);
  }


    // copy properties
    if ($data["type"] >= 1 && isset($_GET["ocopyprops"])) {
      $_GET["ocopyprops"] = utf8Encode($_GET["ocopyprops"]);
      getdata("SELECT parent_id, onto_id FROM class WHERE id = ".$_GET["uid"], $parent_class_id);
      $class_id = $parent_class_id["parent_id"];

      if ($_GET["ocopyprops"] <> $_GET["uid"]){

        // not taxonomycal relation
        $taxonomycal_relation = 1;
        $class_thing = 0;
        $sql_str = "SELECT parent_id FROM class WHERE id = ".$_GET["ocopyprops"];
        getData($sql_str, $parent_class);
        if ($parent_class["parent_id"] == 0)
          $class_thing = 1;
        while ($parent_class["parent_id"] > 0 && $parent_class["parent_id"] <> $_GET["uid"]){
          $sql_str = "SELECT parent_id FROM class WHERE id = ".$parent_class["parent_id"];
          getData($sql_str, $parent_class);
        };
        if ($parent_class["parent_id"] == 0) {
          $sql_str = "SELECT parent_id FROM class WHERE id = ".$_GET["uid"];
          getData($sql_str, $parent_class);
          if ($parent_class["parent_id"] == 0)
            $class_thing = 1;

          while ($parent_class["parent_id"] > 0 && $parent_class["parent_id"] <> $_GET["ocopyprops"]){
            $sql_str = "SELECT parent_id FROM class WHERE id = ".$parent_class["parent_id"];
            getData($sql_str, $parent_class);
          };
          if ($parent_class["parent_id"] == 0) {
            $taxonomycal_relation = 0;
          };
        }

        // not thing
          //on previos operation $class_thing
        if ($taxonomycal_relation == 0 && $class_thing == 0){
          $sql_str = "select t.text, t.description, a.onto_id, a.baseonto_id, a.type, c.domtype_id, c.min, c.max, c.limited FROM class_attr c, attr a, term t WHERE c.attr_id = a.id and a.term_id = t.id and t.lang_id = 0 and t.text not in (SELECT t.text FROM class_attr c, attr a, term t WHERE c.attr_id = a.id and a.term_id = t.id and t.lang_id = 1 and c.class_id = ".$_GET["ocopyprops"].") and c.class_id = ".$_GET["uid"];
          $i_max = getMultdata($sql_str, $attrs);
          for ($i = 0; $i < $i_max; $i++){
            $sql_str = "INSERT INTO term (lang_id, text, description) VALUES (0, '".$attrs[$i]["text"]."', '".$attrs[$i]["description"]."')";
            insertRecord($sql_str);
            $sql_str = "INSERT INTO attr (onto_id, baseonto_id, term_id, Type) VALUES (".$attrs[$i]["onto_id"].", ".$attrs[$i]["baseonto_id"].", ".GetPreviousInsertedID().", ".$attrs[$i]["type"].")";
            insertRecord($sql_str);
            $sql_str = "INSERT INTO class_attr (class_id, attr_id, domtype_id, min, max, limited) VALUES (".$_GET["ocopyprops"].", ".GetPreviousInsertedID().", ".$attrs[$i]["domtype_id"].", ".$attrs[$i]["min"].", ".$attrs[$i]["max"].", ".$attrs[$i]["limited"].")";
            insertRecord($sql_str);

            delete_double_attrs($_GET["ocopyprops"]);
          };
        };
      };
      $_GET["uid"] = $parent_class_id["onto_id"];
      return taxonomy(1, $class_id);
    };

  // copy synonyms
  if ($data["type"] >= 1 && isset($_GET["ocopysyn"])) {
    getdata("SELECT parent_id, onto_id FROM class WHERE id = ".$_GET["uid"], $parent_class_id);
    $class_id = $parent_class_id["parent_id"];
    $_GET["ocopysyn"] = utf8Encode($_GET["ocopysyn"]);
    if ($_GET["ocopysyn"] <> $_GET["uid"]){
      $sql_str = "select term_id FROM class WHERE id = ".$_GET["ocopysyn"];
      getData($sql_str, $term_id);
      $sql_str = "SELECT s.text FROM synonyms s, class c WHERE s.term_id = c.term_id and c.id = ".$_GET["uid"];
      $i_max = getMultdata($sql_str, $synonyms_text);
      for ($i = 0; $i < $i_max; $i++){
        $sql_str = "INSERT INTO synonyms (term_id, text) VALUES (".$term_id["term_id"].", '".$synonyms_text[$i]["text"]."')";
        insertRecord($sql_str);
      };
      delete_double_synonyms($_GET["ocopysyn"]);
      $_GET["uid"] = $parent_class_id["onto_id"];

      return taxonomy(1, $class_id);
    };

  }

}

function createClass() {
  global $_GET, $data, $path, $style, $session_id, $language;
  if ($data["type"] >= 1) { //user
    $filename = $path[$style]["tpl"]."/action.xml";
    $fp = fopen($filename, "r");
    $contents = fread($fp, filesize ($filename));
    fclose ($fp);
    preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

    if (substr($_GET["uid"], 0, 1) == "o"){
      //create upper class to the AO

      getData("SELECT onto_id FROM class where id = ".substr($_GET["uid"], strpos($_GET["uid"], "c") + 1).";", $odatatmp);

      $sql_str = "INSERT INTO term ([lang_id], [text], [description]) VALUES(".
        $language["content"].",'".$session_id."','".$session_id."');";
      insertRecord($sql_str);
      if (getData("SELECT id FROM term where text = '".$session_id."' AND description = '".$session_id."' AND term.lang_id = ".$language["content"].";", $datatmp) == 1) {
        $sql_str = "UPDATE term SET term.text = '".getMessage("Name")."', term.description = '".getMessage("Description").", term.stem_text = "."' WHERE term.id=".$datatmp["id"].
          " AND term.lang_id = ".$language["content"].";";
        insertRecord($sql_str);

        insertRecord ("INSERT INTO class (onto_id, parent_id, base_onto_id, term_id, session_id) VALUES(".
        $odatatmp["onto_id"].", ".substr($_GET["uid"], strpos($_GET["uid"], "c") + 1).", ".substr($_GET["uid"], 1, strpos($_GET["uid"], "c") - 1).", ".$datatmp["id"].", '".$session_id."');");
        $contents = str_replace("%SCCSS%", "1", $contents);
      };
      $tmp_cont = str_replace("%NODE_ID%", "c".$datatmp["id"], $matches[1]);
      $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
      $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
      $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=classdata&amp;sid=".
        $session_id."&amp;uid=c".$datatmp["id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);
      $contents = str_replace($matches[0], $tmp_cont, $contents);
      $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=tax&amp;sid=".
        $session_id."&amp;uid=c".$_GET["uid"], $contents);
      $contents = str_replace("%ACT_ID%", "Add", $contents);
      $contents = str_replace("%PARENT%", $_GET["uid"], $contents);
    }else{
      $_GET["uid"] = substr($_GET["uid"], 1);
        $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$_GET["uid"].")";
        $ontotype = Array();
        getData($sql_tmp, $ontotype);
        if (!($ontotype["type_id"] == "5")){
          if (getData("SELECT onto_id FROM class where id = ".$_GET["uid"].";", $odatatmp) != 1) return "";
          $sql_str = "INSERT INTO term ([lang_id], [text], [description]) VALUES(".$language["content"].",'".$session_id."','".$session_id."');";
          //print ($sql_str);
          insertRecord($sql_str);
          if (getData("SELECT id FROM term where text = '".$session_id."' AND description = '".$session_id."' AND term.lang_id = ".$language["content"].";", $datatmp) == 1) {
            $sql_str = "UPDATE term SET term.text = '".getMessage("Name")."', term.description = '".getMessage("Description").", term.stem_text = ".
              "' WHERE term.id=".$datatmp["id"].
              " AND term.lang_id = ".$language["content"].";";
            insertRecord($sql_str);
            //print ($sql_str);
            $sql = "SELECT base_onto_id FROM class WHERE id = ".$_GET["uid"];
            getData($sql, $base_onto_id);
            insertRecord ("INSERT INTO class (onto_id, parent_id, base_onto_id, term_id, session_id) VALUES(".
              $odatatmp["onto_id"].", ".
              $_GET["uid"].", ".$base_onto_id["base_onto_id"].", ".
              $datatmp["id"].", '".
              $session_id."');");
            if (getData("SELECT id FROM class where session_id = '".
              $session_id."';", $datatmp) == 1) {
              $sql_str = "UPDATE class SET class.session_id = '' WHERE id=".
              $datatmp["id"].";";
              insertRecord($sql_str);
              $contents = str_replace("%SCCSS%", "1", $contents);
            } else {
              $contents = str_replace("%SCCSS%", "0", $contents);
              $datatmp["id"] = 0;
            }
          } else {
            $contents = str_replace("%SCCSS%", "0", $contents);
            $datatmp["id"] = 0;
          }
        }else{
          $contents = str_replace("%SCCSS%", "0", $contents);
          $datatmp["id"] = 0;
        };
      $tmp_cont = str_replace("%NODE_ID%", "c".$datatmp["id"], $matches[1]);
      $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
      $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
      $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=classdata&amp;sid=".
        $session_id."&amp;uid=c".$datatmp["id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations, $tmp_cont);
      $contents = str_replace($matches[0], $tmp_cont, $contents);
      $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=tax&amp;sid=".
        $session_id."&amp;uid=c".$_GET["uid"], $contents);
      $contents = str_replace("%ACT_ID%", "Add", $contents);
      $contents = str_replace("%PARENT%", "c".$_GET["uid"], $contents);
    };
  } else {
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }

  return $contents;
}

function deleteClass() {
  global $_GET, $data, $path, $style, $session_id;
  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);
  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".substr($_GET["uid"], 1).")";
  $ontotype = Array();
  getData($sql_tmp, $ontotype);
  if (!($ontotype["type_id"] == "5")){
    if ($data["type"] >= 1)
      $contents = str_replace("%SCCSS%", deleteClassID(substr($_GET["uid"], 1), 0), $contents);
    else
      $contents = str_replace("%SCCSS%", "0", $contents);
  }else{
    $contents = str_replace("%SCCSS%", "0", $contents);
  };
  $contents = str_replace($matches[0], "", $contents);
  $contents = str_replace("%ACT_ID%", "Delete", $contents);
  $contents = str_replace("%PARENT%", $_GET["uid"], $contents);
  return $contents;
}

function deleteClassID($class_ID, $deleteThing) {
  set_time_limit(5);
  include_once "attr.php";
  include_once "acon.php";
  include_once "compatible.php";
  include_once "hierarchy.php";
  global $term_Thing_ID;
  //Exists? Thing?
  $tmp_sql = ("SELECT term_id FROM class WHERE id = ".$class_ID.";");
  getData($tmp_sql, $classdata);
  if (getData($tmp_sql, $classdata) == 1 && ($deleteThing == 1 || $classdata["term_id"] != $term_Thing_ID)) {
    //find subclasses
    $tmp_sql = ("SELECT id FROM class WHERE parent_id = ".$class_ID.";");
    $i_max = getMultData($tmp_sql, $subclasses);
    //delete subclasses
    for ($i = 0; $i < $i_max; $i++)
      if (deleteClassID($subclasses[$i]["id"], $deleteThing) == 0) return 0;


    //delete attributes
    $tmp_sql = ("SELECT id, attr_id FROM class_attr WHERE class_id = ".$class_ID.";");
    $attrs = getMultData($tmp_sql, $attrdata);
    for ($j = 0; $j < $attrs; $j++) {
      $ID[0] = $class_ID;
      $ID[1] = $attrdata[$j]["attr_id"];
      deleteAttr($ID, -1);
      insertRecord("DELETE FROM class_attr WHERE id=".$attrdata[$j]["id"].";");
    }

    //delete acons
    //echo "SELECT id FROM acon WHERE class1_id=".$class_ID." or class2_id=".$class_ID;
    //echo "SELECT parent_id FROM class WHERE id=".$class_ID;
    $tmp_sql = ("SELECT id FROM acon WHERE class1_id=".$class_ID." or class2_id=".$class_ID);
    $acons = getMultData($tmp_sql, $acondata);
    for ($j = 0; $j < $acons; $j++) {
      getData("SELECT class1_id, class2_id FROM acon WHERE id=".$acondata[$j]["id"], $acons_id);
      $ID[0] = $acons_id["class1_id"];
      $ID[1] = $acons_id["class2_id"];
      //echo "DelAcon:".$ID[0];
      deleteAcon($ID);
    };

    //delete hierarchy
    $tmp_sql = ("SELECT id FROM hierarchy WHERE parent_class_id=".$class_ID." or child_class_id=".$class_ID);
    $hierarchy = getMultData($tmp_sql, $hierarchydata);
    for ($j = 0; $j < $hierarchy; $j++) {
      getData("SELECT parent_class_id, child_class_id FROM hierarchy WHERE id=".$hierarchydata[$j]["id"], $hierarchy_id);
      $ID[0] = $hierarchy_id["parent_class_id"];
      $ID[1] = $hierarchy_id["child_class_id"];
      deleteHierarchy($ID);
    };

    //delete compatible
    $tmp_sql = ("SELECT id FROM compatible WHERE class1_id=".$class_ID." or class2_id=".$class_ID);
    $compatible = getMultData($tmp_sql, $comdata);
    for ($j = 0; $j < $compatible; $j++) {
      getData("SELECT class1_id, class2_id FROM compatible WHERE id=".$comdata[$j]["id"], $compatible_id);
      $ID[0] = $compatible_id["class1_id"];
      $ID[1] = $compatible_id["class2_id"];
      deleteCompatible($ID);
    };

    //delete class
    //echo "Del class:".$class_ID."<br />";
    insertRecord("DELETE FROM class WHERE id=".$class_ID.";");
    if ($classdata["term_id"] != $term_Thing_ID)
      insertRecord("DELETE FROM term WHERE id=".$classdata["term_id"].";");
    return 1;
  }
  return 0;
}

function getClassData($buttons, $comment) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language;
  include "controls.php";

$generationtime = getmicrotime();

  $filename = $path[$style]["tpl"]."/classdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";
  //print("AAA");
  getData("SELECT class.onto_id as onto_id, term.id as termID, term.text as name, term.description as description ".
          "FROM class, term ".
          "WHERE class.id = ".$_GET["uid"].
          " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $ontodata);
  getData("SELECT term.text as name FROM ontology, term ".
          "WHERE ontology.id = ".$ontodata["onto_id"].
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $pontodata);

  $ontodata["name"] = htmlspecialchars($ontodata["name"]);
  $pontodata["name"] = htmlspecialchars($pontodata["name"]);

  $dlg_form = "";

  //$udata = ""; $btns = "";

  if ($data["type"] >= 1 && $ontodata["termID"] != $term_Thing_ID) { //user, class is no Thing
    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);

    $grp0 = new ControlGroup(0, getMessage("Class data"), $matches[1], $datmatches, $optmatches);
    $grp0->addControl("Label", "lname", getMessage("Ontology Name"), $pontodata["name"]);
    $grp0->addControl("Text", "oname", getMessage("Name"), $ontodata["name"]);
    $grp0->addControl("Text", "odescr", getMessage("Description"), $ontodata["description"]);
    if ($comment != "") $grp0->addControl("Label", "", getMessage("Error"), $comment);
    else $grp0->addControl("Label", "", "", $comment);
    //$lablttt = str_replace("%VALUE%", $userdata["type"], $lablttt);
    $grp0->addControl("Button", "obutn", getMessage("Save"), "'0','oname','odescr'");
    $dlg_form .= $grp0->getResult();

    $grp2 = new ControlGroup(2, "Taxonomy operations", $matches[1], $datmatches, $optmatches);

    if ($_GET["cop"] == 1){ // disable long operations
      // classes transference
      $tc_id = $grp2->addControl("Option", "otrans", getMessage("Transference Class"), "");
      getMultdata($la_sql = "SELECT c.id, t.text FROM class c, term t WHERE c.term_id = t.id and c.onto_id=".$ontodata["onto_id"]." ORDER BY t.text", $classes);
      $opts = "";
      $default_class = $_GET["uid"];
      for ($i = 0; $i < count($classes); $i++) {
        if ($classes[$i]["id"] == $default_class)
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id);
        else
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], 0, $tc_id);
      }
      $grp2->addControl("Button", "obutn", getMessage("Save"), "'0_wd_tree', 'otrans'");

      // Join classs, Copy properties
      $tc_id = $grp2->addControl("Option", "ojoin", getMessage("Joined Class"), "");
      $grp2->addControl("Button", "obutn", getMessage("Merge"), "'0_wd_tree', 'ojoin'");
      $tc_id2 = $grp2->addControl("Option", "ocopyprops", getMessage("Copy Properties to Class:"), "");
      $grp2->addControl("Button", "obutn", getMessage("Copy"), "'0_wd_tree', 'ocopyprops'");
      for ($i = 0; $i < count($classes); $i++) {
        if ($classes[$i]["id"] == $_GET["uid"]) {
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id);
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id2);
        }
        else {
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id);
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id2);
        }
      }

      // copy synonyms
      $tc_id = $grp2->addControl("Option", "ocopysyn", getMessage("Copy synonyms"), "");
      getMultdata($la_sql = "SELECT c.id, t.text FROM class c, term t WHERE c.term_id = t.id and c.onto_id=".$ontodata["onto_id"]." ORDER BY t.text", $classes);
      $opts = "";
      // for pashkin
      if (getData("SELECT default_subclass FROM session WHERE session_id = '".$_GET["sid"]."'", $def_class) > 0)
        $default_class = $def_class["default_subclass"];
      else
        $default_class = $_GET["uid"];
      for ($i = 0; $i < count($classes); $i++) {
        if ($classes[$i]["id"] == $default_class)
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], "selected", $tc_id);
        else
          $grp2->addOption($classes[$i]["id"], $classes[$i]["text"], 0, $tc_id);
      }
      $grp2->addControl("Button", "obutn", getMessage("Save"), "'0_wd_tree', 'ocopysyn'");

      $dlg_form .= $grp2->getResult();
    }; // End of disable long operations

  }
  else { //guest
    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $grp0 = new ControlGroup(0, getMessage("Class data"), $matches[1], $datmatches, $optmatches);
    $grp0->addControl("Label", "lname", getMessage("Ontology Name"), $pontodata["name"]);
    $grp0->addControl("Label", "oname", getMessage("Name"), $ontodata["name"]);
    $grp0->addControl("Label", "odescr", getMessage("Description"), $ontodata["description"]);
    $grp0->addControl("Button", "obutn", getMessage("Save"), "'0','oname','odescr'");
    $dlg_form .= $grp0->getResult();
  }
  if ($buttons == 1) {
    $grp1 = new ControlGroup(1, getMessage("Action"), $matches[1], $datmatches, $optmatches);
    $grp1->addControl("Button", "", getMessage("Ontologies"),
      "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".$session_id."&amp;act=ontolist&amp;sel=".$ontodata["onto_id"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations."'");
    $grp1->addControl("Button", "", getMessage("VRML"),
      "'new_wnd', 'index.php?trg=ontoedit&amp;sid=".$session_id."&amp;act=vrml&amp;uid=".$ontodata["onto_id"]."&amp;cid=".$_GET["uid"]."&amp;fld=".$open_folders."&amp;cop=".$class_operations."'");
    $grp1->addControl("Button", "", getMessage("Refresh Ontology"),
      "'wd_tree', 'index.php?trg=ontoedit&amp;act=tax&amp;sid=".$session_id."&amp;uid=".$ontodata["onto_id"]."&amp;sel=0"."&amp;fld=".$open_folders."&amp;cop=".$class_operations."'");
    $dlg_form .= $grp1->getResult();
  }

  return str_replace($matches[0], $dlg_form, $contents);
  //return str_replace($matches[0], $udata.$udata1.$btns, $contents);
}
?>